﻿using CatUtils.Utils.Currency;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;

namespace CatUtils.Utils.SpecialUtils.Html
{
    /// <summary>
    ///创建html代码
    /// </summary>
    public static class HtmlCodeCreate
    {
        /// <summary>
        /// 常见HTML标签名字
        /// </summary>
        public struct HtmlLableType
        {
            /// <summary>
            /// img标签
            /// </summary>
            public static string Img { get; } = "img";

            /// <summary>
            ///div标签
            /// </summary>
            public static string Div { get; } = "div";

            /// <summary>
            /// A标签
            /// </summary>
            public static string A { get; } = "a";

            /// <summary>
            /// P标签
            /// </summary>
            public static string P { get; } = "p";

            /// <summary>
            /// 表单标签
            /// </summary>
            public static string Form { get; } = "form";

            /// <summary>
            /// H标签 仅仅一个h 没有数字
            /// </summary>
            public static string H { get; } = "h";

            /// <summary>
            /// H标签
            /// </summary>
            public static string H1 { get; } = "h1";

            /// <summary>
            /// H标签
            /// </summary>
            public static string H2 { get; } = "h2";

            /// <summary>
            /// H标签
            /// </summary>
            public static string H3 { get; } = "h3";

            /// <summary>
            /// H标签
            /// </summary>
            public static string H4 { get; } = "h4";

            /// <summary>
            /// H标签
            /// </summary>
            public static string H5 { get; } = "h5";

            /// <summary>
            /// H标签
            /// </summary>
            public static string H6 { get; } = "h6";

            /// <summary>
            /// Strong标签
            /// </summary>
            public static string Strong { get; } = "strong";

            /// <summary>
            /// Span标签
            /// </summary>
            public static string Span { get; } = "span";
        }

        /// <summary>
        /// 创建html注释内容
        /// </summary>
        /// <param name="HtmlComment">注释内容</param>
        /// <returns></returns>
        public static string HtmlComment(string HtmlComment)
        {
            return $"<!-- {HtmlComment} -->";
        }

        /// <summary>
        /// 创建HTML标签
        /// </summary>
        /// <param name="LableName"></param>
        /// <returns></returns>
        public static HtmlNode CreateLable(string LableName)
        {
            return CreateLable(LableName, "", new Dictionary<string, string>());
        }

        /// <summary>
        ///创建Html标签
        /// </summary>
        /// <param name="LableName"></param>
        /// <param name="Content"></param>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public static HtmlNode CreateLable(string LableName, string Content, Dictionary<string, string> keyValues)
        {
            HtmlNode htmlNode = new HtmlDocument().CreateElement(LableName);
            try
            {
                if (Content != "")
                    ChangeText(htmlNode, Content);
                foreach (var item in keyValues)
                {
                    htmlNode.Attributes.Add(item.Key, item.Value);
                }
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            return htmlNode;
        }

        /// <summary>
        /// 修改标签文本
        /// </summary>
        /// <param name="htmlNode"></param>
        /// <param name="Content"></param>
        /// <returns></returns>

        public static HtmlNode ChangeText(HtmlNode htmlNode, string Content)
        {
            try
            {
                htmlNode.InnerHtml = HtmlDocument.HtmlEncode(Content);
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }

            return htmlNode;
        }

        /// <summary>
        /// 创建基础的HTML标签，返回<xx>{0}</xx>
        /// </summary>
        /// <param name="LableName"></param>
        /// <returns></returns>
        public static string BasicsCode(string LableName)
        {
            return BasicsCode(LableName, "");
        }

        /// <summary>
        /// 基础标签创建，请参考<!-- {0} {1} -->{2}<!-- {0}-->
        /// </summary>
        /// <param name="LableName">标签名字</param>
        /// <param name="Extend">扩展内容</param>
        /// <returns></returns>
        public static string BasicsCode(string LableName, string Extend)

        {
            return string.Format("<{0}{1}>{2}</{0}>", LableName, Extend, "{0}");
        }

        /// <summary>
        /// 创建属性内容
        /// </summary>
        /// <param name="Lable"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string AttributeCode(string Lable, string Content)
        {
            return string.Format(" {0}=\"{1}\" ", Lable, Content);
        }

        /// <summary>
        /// 创建IMG标签
        /// </summary>
        /// <param name="Imgsrc"></param>
        /// <returns></returns>
        public static string LableImg(string Imgsrc)
        {
            return string.Format(BasicsCode(HtmlLableType.Img, AttributeCode("src", Imgsrc)).Replace("{0}", "")).Replace("></img>", ">");
        }

        /// <summary>
        /// 创建IMG标签
        /// </summary>
        /// <param name="Imgsrc"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableImg(string Imgsrc, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.Img, AttributeCode("src", Imgsrc) + AttributeCode("class", ClassName)), "").Replace("></img>", ">");
        }

        /// <summary>
        ///  创建lightbox支持
        /// </summary>
        /// <param name="Imgurl"></param>
        /// <param name="LightBoxHref"></param>
        /// <returns></returns>
        public static string LableLightBox(string Imgurl, string LightBoxHref)
        {
            return string.Format(BasicsCode(HtmlLableType.A, AttributeCode("data-lightbox", "image") +
                AttributeCode("href", LightBoxHref)
                ), LableImg(Imgurl)).Replace("></img>", ">");
        }

        /// <summary>
        /// 创建P标签
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string LableP(string Content)
        {
            return string.Format(BasicsCode(HtmlLableType.P), Content);
        }

        /// <summary>
        /// 创建P标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableP(string Content, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.P, AttributeCode("class", ClassName)), Content);
        }

        /// <summary>
        /// 创建Div标签
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string LableDiv(string Content)
        {
            return string.Format(BasicsCode(HtmlLableType.Div), Content);
        }

        /// <summary>
        /// 创建Div标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableDiv(string Content, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.Div, AttributeCode("class", ClassName)), Content);
        }

        /// <summary>
        /// 创建A标签
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string LableA(string Content)
        {
            return string.Format(BasicsCode(HtmlLableType.A), Content);
        }

        /// <summary>
        /// 创建A标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="Href"></param>
        /// <returns></returns>
        public static string LableA(string Content, string Href)
        {
            return string.Format(BasicsCode(HtmlLableType.A, AttributeCode("href", Href)), Content);
        }

        /// <summary>
        /// 创建A标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="Href"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableA(string Content, string Href, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.A, AttributeCode("class", ClassName) + AttributeCode("href", Href)), Content);
        }

        /// <summary>
        /// 创建新窗口打开的A标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="Href"></param>
        /// <returns></returns>
        public static string LableANewWindow(string Content, string Href)
        {
            return string.Format(BasicsCode(HtmlLableType.A, AttributeCode("href", Href) + AttributeCode("target", "view_window")), Content);
        }

        /// <summary>
        /// 创建新窗口打开的A标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="Href"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableANewWindow(string Content, string Href, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.A, AttributeCode("class", ClassName) + AttributeCode("href", Href) + AttributeCode("target", "view_window")), Content);
        }

        /// <summary>
        /// 创建H标签 可设置参数1-6
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="HLableCount"></param>
        /// <returns></returns>
        public static string LableH(string Content, int HLableCount)
        {
            return string.Format(BasicsCode(HtmlLableType.H + HLableCount), Content);
        }

        /// <summary>
        ///  创建H标签 可设置参数1-6
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="HLableCount"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableH(string Content, int HLableCount, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.H + HLableCount, AttributeCode("class", ClassName)), Content);
        }

        /// <summary>
        /// 加粗标签
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string LableStrong(string Content)
        {
            return string.Format(BasicsCode(HtmlLableType.Strong), Content);
        }

        /// <summary>
        /// 加粗标签
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="ClassName"></param>
        /// <returns></returns>
        public static string LableStrong(string Content, string ClassName)
        {
            return string.Format(BasicsCode(HtmlLableType.Strong, AttributeCode("class", ClassName)), Content);
        }
    }
}